<?php
/**
 * Nette Framework - InstantClientScript template.
 *
 * @author     David Grudl
 * @license    http://nette.org/license  Nette license
 * @link       http://nette.org
 *
 * @param      string    $formName
 * @param      this      InstantClientScript
 * @return     void
 */

namespace Nette\Forms;

use Nette;
?>
<!-- Nette Form validator -->

<script type="text/javascript">/*<![CDATA[*/
var nette = nette || {};
nette.getValue = function (a) {
    if (a) {
        if (!a.nodeName) {
            for (var b = 0, c = a.length; b < c; b++)if (a[b].checked)return a[b].value;
            return null
        }
        if (a.nodeName.toLowerCase() === "select") {
            b = a.selectedIndex;
            var d = a.options;
            if (b < 0)return null; else if (a.type === "select-one")return d[b].value;
            b = 0;
            a = [];
            for (c = d.length; b < c; b++)d[b].selected && a.push(d[b].value);
            return a
        }
        if (a.type === "checkbox")return a.checked;
        return a.value.replace(/^\s+|\s+$/g, "")
    }
};
nette.getFormValidators = function (a) {
    a = a.getAttribute("id").value;
    return this.forms[a] ? this.forms[a].validators : []
};
nette.validateControl = function (a) {
    var b = this.getFormValidators(a.form)[a.name];
    return b ? b(a) : null
};
nette.validateForm = function (a) {
    var isOk = true;
    var errorElements = [];
    var b = a.form || a, c = this.getFormValidators(b);
    for (var d in c) {
        var e = c[d](a);
        if (e) {
            isOk = false;
            errorElements.push({"element" : d, "message" : e});

            //console.log(d); // log input name
            //console.log(e); // log message

            b[d].focus && b[d].focus();
        }
    }
    // call validate if it's defined
    if ((typeof(validate) == typeof(Function))) {
        validate(errorElements);
    }
    return isOk;
};
nette.toggle = function (a, b) {
    var c = document.getElementById(a);
    if (c)c.style.display = b ? "" : "none"
};
/*]]>*/</script>


<script type="text/javascript">
    /* <![CDATA[ */

    nette.forms = nette.forms || { };

    nette.forms[<?php echo $formName ?>] = {
        validators: {
<?php $count = count($this->validateScripts); ?>
<?php foreach ($this->validateScripts as $name => $validateScript): ?>
<?php echo json_encode((string) $name) ?>: function(sender) {
            var res, val, form = sender.form || sender;
<?php echo Nette\String::indent($validateScript, 3) ?>
        }<?php echo--$count ? ',' : '' ?>

<?php endforeach ?>
    },

    toggle: function(sender) {
        var visible, res, form = sender.form || sender;
<?php echo Nette\String::indent($this->toggleScript, 2) ?>
    }
}


<?php if ($this->toggleScript): ?>
    nette.forms[<?php echo $formName ?>].toggle(document.getElementById(<?php echo $formName ?>));
<?php endif ?>

/* ]]> */
</script>

<!-- /Nette Form validator -->
